const express = require('express')
const fs = require('fs')

const Course = require('../../../models/Course')
const {
  uploadURI
} = require('../../../config/key')

const router = express.Router()

router.get('/', (req, res) => {
  res.send('hello course')
})

/**
 * @description 返回所有的免费课程
 * @router /course/freeCourse
 * @method GET
 */
router.get('/freeCourse', (req, res) => {
  Course.find({
      price: '免费'
    })
    .then(courseList => {
      res.send({
        status: 1,
        msg: '获取成功',
        data: courseList
      })
    })
    .catch(err => {
      res.send({
        status: 0,
        msg: '获取失败',
        data: null
      })
      throw err
    })
})


/**
 * @description 返回各种课程列表
 * @router /api/course/courseList
 * @method GET
 */
router.get('/courseList', (req, res) => {
  const type = req.query.type
  Course.find({
      type
    })
    .then(courseList => {
      let ret = []

      for (let i = 0; i < courseList.length; i++) {

        let courseSrc = {}
        let courseURI = courseList[i].courseURI
        let str = courseURI.substring(21, courseURI.length)
        let moviesArr = fs.readdirSync(`${uploadURI}${str}`)

        courseSrc.price = courseList[i].price
        courseSrc.buyNum = courseList[i].buyNum
        courseSrc.courseType = courseList[i].courseType
        courseSrc.evaluate = [...courseList[i].evaluate]
        courseSrc.chatMsg = [...courseList[i].chatMsg]
        courseSrc.id = courseList[i]._id
        courseSrc.coursename = courseList[i].coursename
        courseSrc.description = courseList[i].description
        courseSrc.teacherId = courseList[i].teacherId
        courseSrc.courseURI = courseList[i].courseURI
        courseSrc.type = courseList[i].type
        courseSrc.teacherName = courseList[i].teacherName

        for (let j = 0; j < moviesArr.length; j++) {
          moviesArr[j] = courseURI + '/' + moviesArr[j]
        }

        courseSrc.src = [...moviesArr]
        ret.push({
          courseSrc
        })
      }
      res.send({
        status: 1,
        msg: '请求成功',
        data: {
          ret
        }
      })
    })
    .catch(err => {
      throw err
    })
})


/**
 * @description 课程评论
 * @router /api/course/comment
 * @method POST
 */
router.post('/comment', (req, res) => {
  const data = req.body
  Course.findById(data.courseId)
    .then(course => {
      course.evaluate.push({
        evaluateAvatar: data.evaluateAvatar,
        evaluateNickname: data.evaluateNickname,
        evaluateContent: data.evaluateContent,
        score: data.score
      })
      course.save()
        .then(course => {
          res.send(data)
        })
        .catch(err => {
          throw err
        })
    })
    .catch(err => {
      throw err
    })
})

/**
 * @description 获取课程评论
 * @router /api/course/comment
 * @method GET
 */
router.get('/comment', (req, res) => {
  const data = req.headers

  Course.findById(data.courseid)
    .then(course => {
      res.send(course.evaluate)
    })
    .catch(err => {
      throw err
    })
})


/**
 * 用户搜索课程接口
 * @description user search course
 * @router /api/course/search
 * @method post
 */
router.post('/search', (req, res) => {
  const query = new RegExp(req.body.keyword, 'ig')
  Course.where('coursename', query)
  .then(courseList => {
    let ret = []

    for (let i = 0; i < courseList.length; i++) {

      let courseSrc = {}
      let courseURI = courseList[i].courseURI
      let str = courseURI.substring(21, courseURI.length)
      let moviesArr = fs.readdirSync(`${uploadURI}${str}`)

      courseSrc.price = courseList[i].price
      courseSrc.buyNum = courseList[i].buyNum
      courseSrc.courseType = courseList[i].courseType
      courseSrc.evaluate = [...courseList[i].evaluate]
      courseSrc.chatMsg = [...courseList[i].chatMsg]
      courseSrc.id = courseList[i]._id
      courseSrc.coursename = courseList[i].coursename
      courseSrc.description = courseList[i].description
      courseSrc.teacherId = courseList[i].teacherId
      courseSrc.courseURI = courseList[i].courseURI
      courseSrc.type = courseList[i].type
      courseSrc.teacherName = courseList[i].teacherName

      for (let j = 0; j < moviesArr.length; j++) {
        moviesArr[j] = courseURI + '/' + moviesArr[j]
      }

      courseSrc.src = [...moviesArr]
      ret.push({
        courseSrc
      })
    }
    res.send({
      status: 1,
      msg: '请求成功',
      data: {
        ret
      }
    })
  })
    .catch(err => {
      throw err
    })
})


module.exports = router